package xyc.szk.sjxy.filter;
/*
 * 作者：zjLiu
 * 日期：2021-05-21
 * 公司：数学与计算机学院
 */

import xyc.szk.sjxy.pojo.Users;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(value = {"/admin/add.jsp","/admin/del.jsp","/admin/edit.jsp"})
public class LoginFilter  implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //验证用户是否登录 或登录 则放行，否则拦截 重定向到登录页面
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Users loginInfo = (Users) request.getSession().getAttribute("loginInfo");
        if (loginInfo!=null) {
            //已登录 放行
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
//            没有登录 重定向到login.jsp
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
    }
}
